<?php

namespace App\Api\Controllers;

use App\Models\MiniUser;
use App\Models\RoomCheckIn;
use App\Models\Rooms;
use Illuminate\Http\Request;

class RoomController extends BaseController
{

    public function getCheckCode(Request $request){
        $openid = $request->get("openid");
        if(!$openid){
            return $this->fail(402,"缺少参数",[]);
        }
    }
    public function checkIn(Request $request){
        $code = $request->get("code");
        $user_id = $request->get("user_id");
        if(!$code || !$user_id){
            return $this->fail(402,"缺少参数",[]);
        }
        $user_info = MiniUser::where("id",$user_id)->first();

        if(!$user_info){
            return $this->fail(404,"用户信息未查询到",[]);
        }
        $room_check_info = RoomCheckIn::where([["check_code",$code],["status",1]])->first();
        if(!$room_check_info){
            return $this->fail(404,"请确认入住码是否输入正确",[]);
        }
        if($room_check_info->openid!=$user_info->openid){
            // 朋友入住
            $check_info = new RoomCheckIn();
            $check_info->room_id = $room_check_info->room_id;
            $check_info->room_num = $room_check_info->room_num;
            $check_info->pid = $room_check_info->id;
            $check_info->user_name = $user_info->nickname;
            $check_info->openid = $user_info->openid;
            $check_info->sex = 0;
            $check_info->user_phone = $user_info->phone;
            $check_info->check_code = $code;
            $check_info->in_date = $room_check_info->in_date;;
            $check_info->out_date = $room_check_info->out_date;;
            $check_info->status = $room_check_info->status;
            $check_info->updated_at = date("Y-m-d H:i:s");
            $check_info->created_at = date("Y-m-d H:i:s");
            $check_info->save();
        }
        $room_info = Rooms::where("room_num",$room_check_info->room_num)->first();
        if(!$room_info){
            return $this->fail(404,"房间信息未查询到",[]);
        }
        return $this->success(200,"入住成功",["room_check_info"=>$room_check_info->openid==$user_info->openid?$room_check_info:$check_info,"rooms_set_info"=>$room_info]);

    }
}
